package com.campus.shop.service.impl;

import com.campus.shop.controller.web.vo.GoodsDetailVo;
import com.campus.shop.controller.web.vo.GoodsListVo;
import com.campus.shop.controller.web.vo.SaleUserInfoVo;
import com.campus.shop.entity.Goods;
import com.campus.shop.entity.GoodsCategory;
import com.campus.shop.mapper.GoodsCategoryMapper;
import com.campus.shop.mapper.GoodsMapper;
import com.campus.shop.mapper.UserSaleInfoMapper;
import com.campus.shop.service.GoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.campus.shop.common.ConstantProperties;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;

/**
 * 商品服务类
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private GoodsCategoryMapper goodsCategoryMapper;
    @Autowired
    private UserSaleInfoMapper userSaleInfoMapper;

    @Override
    public HashMap<String, Object> pageList(Integer page, Integer cateId, String goodsName) {
        Integer offset = (page - 1) * 10;
        HashMap<String,Object> res = new HashMap<>();
        List<GoodsListVo> pageList = goodsMapper.getPageList(ConstantProperties.goods_limit, offset, cateId, goodsName);
        for (GoodsListVo goodsListVo : pageList){
            goodsListVo.setGoodsImg(ConstantProperties.hostName + goodsListVo.getGoodsImg());
        }

        Integer total = goodsMapper.getPageTotal(cateId,goodsName);
        res.put("list",pageList);
        res.put("total",total);
        return res;
    }

    @Override
    public List<GoodsCategory> goodCateList() {
        return goodsCategoryMapper.goodsCateList();
    }

    @Override
    public GoodsDetailVo findGoodById(Integer goodsId) {
        return goodsMapper.findGoodById(goodsId);
    }

    @Override
    public SaleUserInfoVo getGoodsSaleInfo(Integer userId) {
        SaleUserInfoVo saleInfo = userSaleInfoMapper.getOneByUid(userId);
        if(Objects.isNull(saleInfo)){
            SaleUserInfoVo saleUserInfoVo = new SaleUserInfoVo();
            saleUserInfoVo.setSaleName("未知");
            saleUserInfoVo.setStoreName("未知");
            saleUserInfoVo.setPjScore(4.5);
            saleUserInfoVo.setShScore(4.5);
            saleUserInfoVo.setWlScore(4.5);
            return saleUserInfoVo;
        }
        return saleInfo;
    }
}

